//
//  FlightHeaderView.m
//  旅游趣吧
//
//  Created by apple on 15/10/14.
//  Copyright © 2015年 lampshade. All rights reserved.
//

#import "FlightHeaderView.h"
#import "SqliteHelper.h"

@interface FlightHeaderView()
@property (nonatomic, weak) UIButton *btn_Click;
@property (nonatomic, weak) UILabel *lbl_offTime;
@property (nonatomic, weak) UILabel *lbl_arrTime;
@property (nonatomic, weak) UILabel *lbl_offTerm;
@property (nonatomic, weak) UILabel *lbl_arrTerm;
@property (nonatomic, weak) UILabel *lbl_planeType;
@property (nonatomic, weak) UILabel *lbl_carrierName;
@property (nonatomic, weak) UILabel *lbl_price;
@property (nonatomic, weak) UIButton *image_arrow;

@end

@implementation FlightHeaderView

+ (instancetype)headerViewForFlight:(UITableView *)tableView{
    static NSString *identity = @"section_cell";
    FlightHeaderView *headerVw = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identity];
    if (headerVw == nil) {
        headerVw = [[FlightHeaderView alloc]initWithReuseIdentifier:identity];
    }
    headerVw.contentView.backgroundColor = [UIColor lightTextColor];
    return headerVw;
}

#pragma mark - ***************重写headerView的initWithReuseIdentifier方法***************
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(openCell)];
        recognizer.numberOfTapsRequired = 1;
        [self addGestureRecognizer:recognizer];
        
        UIButton *sectionBtn = [[UIButton alloc]init];
        [self.contentView addSubview:sectionBtn];
        self.btn_Click = sectionBtn;
        [self.btn_Click setBackgroundImage:[UIImage imageNamed:@"FlightHeaderView"] forState:UIControlStateNormal];
        
        UIButton *btn_image = [[UIButton alloc]init];
        [self.contentView addSubview:btn_image];
        self.image_arrow = btn_image;
        [self.image_arrow setBackgroundImage:[UIImage imageNamed:@"arrow-right"] forState:UIControlStateNormal];
        
        UILabel *offTime = [[UILabel alloc]init];
        [self.contentView addSubview:offTime];
        self.lbl_offTime = offTime;
        self.lbl_offTime.font = [UIFont systemFontOfSize:16];
        [self.lbl_offTime setTextAlignment:NSTextAlignmentCenter];
        
        UILabel *arrTime = [[UILabel alloc]init];
        [self.contentView addSubview:arrTime];
        self.lbl_arrTime = arrTime;
        self.lbl_arrTime.font = [UIFont systemFontOfSize:16];
        [self.lbl_arrTime setTextAlignment:NSTextAlignmentCenter];
        
        UILabel *offTerm = [[UILabel alloc]init];
        [self.contentView addSubview:offTerm];
        self.lbl_offTerm = offTerm;
        self.lbl_offTerm.font = [UIFont systemFontOfSize:12];
        
        UILabel *arrTerm = [[UILabel alloc]init];
        [self.contentView addSubview:arrTerm];
        self.lbl_arrTerm = arrTerm;
        self.lbl_arrTerm.font = [UIFont systemFontOfSize:12];
        
        UILabel *planeType = [[UILabel alloc]init];
        [self.contentView addSubview:planeType];
        self.lbl_planeType = planeType;
        self.lbl_planeType.font = [UIFont systemFontOfSize:12];
        
        UILabel *carrierName = [[UILabel alloc]init];
        [self.contentView addSubview:carrierName];
        self.lbl_carrierName = carrierName;
        self.lbl_carrierName.font = [UIFont systemFontOfSize:12];
        
        UILabel *price = [[UILabel alloc]init];
        [self.contentView addSubview:price];
        self.lbl_price = price;
        self.lbl_price.font = [UIFont systemFontOfSize:16];
        [self.lbl_price setTextAlignment:NSTextAlignmentRight];
        [self.lbl_price setTextColor:[UIColor redColor]];
    }
    return self;
}

#pragma mark - ***************设置headerView视图各控件的数据***************
- (void)setFlightModels:(FlightListModel *)flightModels{
    _flightModels = flightModels;
    
    //查询数据库中三字码所对应的机场名称
    NSString *off_AirPort = [[SqliteHelper alloc]queryCityName:flightModels.DptAirport];
    NSString *arr_AirPort = [[SqliteHelper alloc]queryCityName:flightModels.ArrAirport];
    
    //处理时间显示
    NSMutableString *dptTime = [[NSMutableString alloc]initWithString:flightModels.DptTime];
    [dptTime insertString:@":" atIndex:2];
    
    NSMutableString *arrTime = [[NSMutableString alloc]initWithString:flightModels.ArrTime];
    [arrTime insertString:@":" atIndex:2];
    
    if ([flightModels.PlanTypeName isEqualToString:@""]) {
        flightModels.PlanTypeName = @"暂无该飞机类型";
    }
    
    NSString *price = @"￥";
    price = [price stringByAppendingString:flightModels.YPrice];
    
    self.lbl_offTime.text = dptTime;
    self.lbl_offTerm.text = [off_AirPort stringByAppendingString:flightModels.DptTerm];
    self.lbl_arrTime.text = arrTime;
    self.lbl_arrTerm.text = [arr_AirPort stringByAppendingString:flightModels.ArrTerm];
    self.lbl_carrierName.text = [flightModels.CarrierName stringByAppendingString:@"|"];
    self.lbl_planeType.text = flightModels.PlanTypeName;
    self.lbl_price.text = price;
    
}

#pragma mark - ***************点击headerView时设置代理***************
- (void)openCell{
    
    self.flightModels.Visible = !self.flightModels.isVisible;
    if ([self.delegate respondsToSelector:@selector(flightBtnDidClick:)]) {
        [self.delegate flightBtnDidClick:self];
    }
}

#pragma mark - ***************当加载headerView时，自动进行布局***************
- (void)layoutSubviews{
    CGFloat controlW = self.bounds.size.width/4;
    CGFloat controlH = self.bounds.size.height;
    CGFloat controlX = controlW;
    self.btn_Click.frame = self.bounds;
    self.lbl_offTime.frame = CGRectMake(15, 0, controlW, controlH/3);
    self.image_arrow.frame = CGRectMake(controlW/4*5, controlH/10, controlW/2, controlH/20);
    self.lbl_arrTime.frame = CGRectMake(controlX*2, 0, controlW, controlH/3);
    self.lbl_offTerm.frame = CGRectMake(15, controlH/3, controlW*4/3, controlH/3);
    self.lbl_arrTerm.frame = CGRectMake(controlX*2, controlH/3, controlW*4/3, controlH/3);
    self.lbl_carrierName.frame = CGRectMake(15, controlH/3*2, controlW, controlH/3);
    self.lbl_planeType.frame = CGRectMake(controlX-13, controlH/3*2, controlW*3, controlH/3);
    self.lbl_price.frame = CGRectMake(controlX*3, (controlH-15)/2, controlW-15, controlH/4);
    

}
@end
